package com.fuhui.controller.manager;

import com.fuhui.dao.txm.AppconfigApplistMapper;
import com.fuhui.dao.txm.AppconfigMapper;
import com.fuhui.entity.txm.Appconfig;
import com.fuhui.entity.txm.AppconfigApplist;
import com.fuhui.util.DateFromat;
import com.fuhui.util.ReturnJson;
import com.fuhui.util.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("schAppConfig")
public class SchoolAppConfigManage {

    @Autowired
    private AppconfigMapper appconfigMapper;

    @Autowired
    private AppconfigApplistMapper appconfigApplistMapper;

    /**
     * 添加配置
     * @param request
     * @return
     */
    @RequestMapping("add")
    public Object add(HttpServletRequest request) {
        Map<String,Object> map = new HashMap<String, Object>();
        SnowFlake snowFlake = new SnowFlake(2, 3);
        Date date = new Date();
        String idDel = "0";
        long iConfigid = snowFlake.nextId();
        long I_IDX = snowFlake.nextId();
        long iSchoolid = 0;
        String name = ""; //request.getParameter("name");
        String schoolId = "1"; //request.getParameter("schoolId");
        String range = "0";//request.getParameter("range");
        Integer intRange = 0;
        String rangeIds = "";//request.getParameter("rangeIds");
        String remark = "";//request.getParameter("remark");
        String status = "0";//request.getParameter("status");
        Integer intStatus = 0;
        try {
            name = request.getParameter("name");
            schoolId = request.getParameter("schoolId");
            range = request.getParameter("range");
            rangeIds = request.getParameter("rangeIds");
            remark = request.getParameter("remark");
            status = request.getParameter("status");
        }catch (Exception e){
            System.out.println("提交参数异常！");
        }
        System.out.println("**schoolId**"+schoolId);
        if (schoolId == null || schoolId.equals("")){
            iSchoolid = 1;
        }else {
            iSchoolid = Long.valueOf(schoolId);
        }
        if (status == null || status.equals("")){
            intStatus = 1;
        }else {
            intStatus = Integer.parseInt(range);
        }
        if (range == null || range.equals("")){
            intRange = 0;
        }else {
            intRange = Integer.parseInt(range);
        }
        if (name == null || name.equals("")){
            name = "admin";
        }
        Appconfig appconfig = new Appconfig();
        appconfig.setiConfigid(iConfigid);
        appconfig.setiSchoolid(iSchoolid);
        appconfig.setcName(name);
        appconfig.setcRangeids(rangeIds);
        appconfig.setcRemark(remark);
        appconfig.settCtime(DateFromat.datetimeChange(date));
        appconfig.settUtime(DateFromat.datetimeChange(date));
        appconfig.setiCuserid(iConfigid);//操作人id登录后取值（创建人）
        appconfig.setiUuserid(iConfigid);//操作人id登录后取值（更新人）
        appconfig.setiRange(intRange);
        appconfig.setiStatus(intStatus);

        int result = appconfigMapper.add(appconfig);
        if (result > 0 ){
            map.put("iConfigid",iConfigid);
            AppconfigApplist txmApplist = new AppconfigApplist();
            txmApplist.setiConfigid(iConfigid);
            txmApplist.setiIdx(I_IDX);
            txmApplist.setiAppid(iConfigid);
            txmApplist.setiCuserid(iConfigid);
            Date txmdate = new Date();
            txmApplist.settCtime(DateFromat.datetimeChange(txmdate));
            int jg = saveAppList(idDel,txmApplist);
            if (jg > 0){
                return iConfigid;
            }
        }
        return 0;
    }


    /**
     * 更新配置
     * @param request
     * @return
     */
    @RequestMapping("update")
    public Object update(HttpServletRequest request) {
        Map<String,Object> map = new HashMap<String, Object>();
        SnowFlake snowFlake = new SnowFlake(2, 3);
        Date date = new Date();
        String idDel = "0";
        long iConfigid = Long.valueOf("222763776917909504");
        long I_IDX = Long.valueOf("222764811182944257");
        long iSchoolid = 0;
        Integer finalResut = 0;
        String name = ""; //request.getParameter("name");
        String schoolId = "1"; //request.getParameter("schoolId");
        String range = "0";//request.getParameter("range");
        Integer intRange = 0;
        String rangeIds = "";//request.getParameter("rangeIds");
        String remark = "";//request.getParameter("remark");
        String status = "0";//request.getParameter("status");
        Integer intStatus = 0;
        try {
            name = request.getParameter("name");
            schoolId = request.getParameter("schoolId");
            range = request.getParameter("range");
            rangeIds = request.getParameter("rangeIds");
            remark = request.getParameter("remark");
            status = request.getParameter("status");
        }catch (Exception e){
            System.out.println("提交参数异常！");
        }
        System.out.println("**schoolId**"+schoolId);
        if (schoolId == null || schoolId.equals("")){
            iSchoolid = 1;
        }else {
            iSchoolid = Long.valueOf(schoolId);
        }
        if (status == null || status.equals("")){
            intStatus = 1;
        }else {
            intStatus = Integer.parseInt(range);
        }
        if (range == null || range.equals("")){
            intRange = 1;
        }else {
            intRange = Integer.parseInt(range);
        }
        if (name == null || name.equals("")){
            name = "user";
        }

        Appconfig appconfig = new Appconfig();
        appconfig.setiConfigid(iConfigid);
        appconfig.setiSchoolid(iSchoolid);
        appconfig.setcName(name);
        appconfig.setcRangeids(rangeIds);
        appconfig.setcRemark(remark);
        appconfig.settUtime(DateFromat.datetimeChange(date));
        appconfig.setiUuserid(iConfigid);//操作人id登录后取值（更新人）
        appconfig.setiRange(intRange);
        appconfig.setiStatus(intStatus);
        int result = appconfigMapper.update(appconfig);
        if (result > 0 ){
            map.put("iConfigid",iConfigid);
            AppconfigApplist txmApplist = new AppconfigApplist();
            txmApplist.setiConfigid(iConfigid);
            txmApplist.setiIdx(I_IDX);
            txmApplist.setiAppid(iConfigid);
            txmApplist.setiCuserid(iConfigid);
            Date txmdate = new Date();
            txmApplist.settCtime(DateFromat.datetimeChange(txmdate));
            int jg = updateAppList(txmApplist);
            if (jg > 0){
                finalResut = 1;
            }
        }
        System.out.println("**finalResut**"+finalResut);
        return ReturnJson.returnSuccessMap(map);
    }



    /**
     * 表TxmAppconfigApplist中添加数据
     * @param isDel
     * @param appconfigApplist
     * @return
     */
    private int saveAppList(String isDel,AppconfigApplist appconfigApplist){
        //isDel用于判断是否删除T_XM_AppConfig_AppListLog中的数据
        /*if (isDel == "1" || isDel.equals("1") ){

        }else {

        }*/
        int k = appconfigApplistMapper.saveAppList(appconfigApplist);
        if (k > 0){
            return 1;
        }else {
            return 0;
        }
    }

    /**
     * 更新副表txmAppconfigApplist
     * @param appconfigApplist
     * @return
     */
    private int updateAppList(AppconfigApplist appconfigApplist){
        int k = appconfigApplistMapper.updateByPrimaryKey(appconfigApplist);
        if (k > 0){
            return 1;
        }else {
            return 0;
        }
    }

}
